home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / pctj8412.arc / BIN32.ASM < prev    next >
Assembly Source File  |  1986-01-18  |  4KB  |  85 lines

  1. BIN32    PROC     NEAR 
  2. ;*************************************************************
  3. ;                  converts an ASCII decimal string to a 
  4. ;                  signed 32-bit binary integer in DX:AX 
  5. ;                  input--DI points to end of string 
  6. ;                         SI points to start 
  7. ;                  output--CX if negative, number of digits
  8. ;                          right of decimal point 
  9. ;                          SI points to next unconverted char
  10. ;*************************************************************
  11.          PUSH     BP 
  12.          SUB      DX,DX 
  13. ;                            get sign 
  14.          MOV      CX,DX           ;assume positive sign 
  15.          MOV      AL,[SI]         ;put first char in AL 
  16.          CMP      AL," "          ;is it blank? positive sign
  17.          JE       BIN32A 
  18.          CMP      AL,"+"          ;is it plus sign? 
  19.          JE       BIN32A 
  20.          CMP      AL,"-"          ;is it minus sign? 
  21.          JNE      BIN32B 
  22.          DEC      CX              ;sign negative-set flag inCX 
  23. BIN32A:  INC      SI              ;point to next char 
  24. BIN32B:  PUSH     CX              ;save sign 
  25. ;                            convert string 
  26.          MOV      AX,DX           ;0-initial NUMber in DX:AX
  27.          MOV      CL,127          ;max 127 places left of decimal
  28.                                   ;CL=COUNT places right ofdecimal 
  29. ;                            get ASCII digit 
  30. BIN32C:  MOV      CH,[SI]         ;next char in CH 
  31.          CMP      CH,"9"          ;if CH > "9" not a digit 
  32.          JA       BIN32H 
  33.          SUB      CH,48           ;convert to binary 
  34.          JB       BIN32H          ;if CH < "0" not a digit 
  35.          DEC      CL              ;found digit--decrement COUNT
  36. ;                            multiply NUM * 10 
  37.          MOV      BP,CX           ;save CX in BP 
  38.          SUB      CX,CX           ;decimal 10 in CX:BX 
  39.          MOV      BX,10 
  40.          CALL     MUL32           ;MUL32 returns DX:AX:CX:BX
  41. ;                            check for overflow--over 2^31-1
  42.          OR       AX,AX           ;if AL not zero, product ▄j▄î         JNZ      BIN32I          ;is > or = 2^32-overflow 
  43.          OR       CX,CX           ;if CX sign bit set, product
  44.          JS       BIN32I          ;is > or = 2^31-overflow 
  45. ;                            add in this digit 
  46.          XCHG     CX,BP           ;digit & COUNT in CX 
  47.          MOV      AL,CH           ;this digit in DX:AX 
  48.          ADD      AX,BX           ;add NUM*10 from CX:BX 
  49.          ADC      DX,BP 
  50.          JC       BIN32I          ;check for overflow again
  51. ;                            get next ASCII character 
  52. BIN32D:  INC      SI              ;point to next char 
  53.          CMP      SI,DI           ;is there another one? 
  54.          JB       BIN32C          ;if so, go get it 
  55. ;                            set final decimal place count 
  56. BIN32E:  XCHG     AX,CX           ;convert count in CL 
  57.          CBW                      ;to word in AX 
  58.          XCHG     AX,CX           ;put count back in CX 
  59.          OR       CX,CX           ;there are no decimals 
  60.          JS       BIN32F          ;if count in CX is positive,
  61.          SUB      CX,CX           ;so make CX zero 
  62. ;                            restore sign 
  63. BIN32F:  POP      BX 
  64.          OR       BX,BX 
  65.          JNS      BIN32G           ;if sign was negative 
  66.          NOT      AX               ;negate NUM 
  67.          NOT      DX 
  68.          ADD      AX,1 
  69.          ADC      DX,0 
  70. ;                            wrap up 
  71. BIN32G:  SUB      BX,BX            ;BX=0  status ok 
  72.          POP      BP 
  73.          RET 
  74. ;                            found non-numeric character 
  75. BIN32H:  CMP      CH,-2            ;is it decimal point (46-48)?
  76.          JNE      BIN32E           ;if not, finish 
  77.          SUB      CL,CL            ;decimal so,set COUNT tozero 
  78.          JMP      BIN32D           ;and get next char 
  79. ;                            overflow 
  80. BIN32I:  POP      BX               ;remove sign from stack 
  81.          POP      BP               ;restore BP 
  82.          MOV      BX,1             ;BX=1 overflow flag 
  83.          RET 
  84. BIN32    ENDP 
  85.